import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
const path = require('path');

export default (appInfo: EggAppInfo) => {
  const config: PowerPartial<EggAppConfig> = {};

  config.keys = appInfo.name + '_1567236540913_9529';

  config.sequelize = {
    // 数据库类型
    dialect: 'mysql',
    // 数据库地址
    host: '127.0.0.1',
    // 端口号
    port: 3306,
    // 连接数据库
    database: 'sharesites',
    // 用户名
    username: 'root',
    // 密码
    password: 'root',
    define: {
      underscored: true,
      charset: 'utf8'
    }
  };

  config.view = {
    root: [
      path.join(appInfo.baseDir, 'app/view')
    ].join(','),
    // 使用模板引擎
    defaultViewEngine: 'nunjucks',
    // 默认后缀
    defaultExtension: '.nj',
    mapping: {
      // 指定使用模板引擎的后缀
      '.nj': 'nunjucks',
    },
  };

  config.passport = {
    enable: true,
    package: 'egg-passport',
  };

  // 开发环境关闭CSRF
  config.security = {
    csrf: {
      enable: false,
    },
    // domainWhiteList: ['http://127.0.0.1:3000', 'http://127.0.0.1:9999']
  };

  config.cors = {
    origin: ctx => {
      // tslint:disable-next-line:typedef-whitespace
      let whiteList: String[] = [];
      whiteList = ['http://127.0.0.1:8082', 'http://127.0.0.1:9999', 'http://127.0.0.1:3000', 'http://localhost:9999', 'http://localhost:3000'];

      let url = ctx.header.referer.substr(0, ctx.header.referer.length - 1);
      if (whiteList.includes(url)) {
        return url;
      }
      return 'http://127.0.0.1:3000';
    },
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
    credentials: true,
  };

  config.static = {
    prefix: '/static/',
    dir: [
      path.join(appInfo.baseDir, 'app/public'),
      path.join(appInfo.baseDir, 'app/lib'),
      path.join(appInfo.baseDir, 'app/view/plugin')
    ]// 多静态文件入口
  };

  config.redis = {
    client: {
      port: 6379,          // Redis port
      host: '127.0.0.1',   // Redis host
      password: '',
      db: 0
      /*  admin: {
         port: 6379,          // Redis port
         host: '127.0.0.1',   // Redis host
         password: '',
         db: 0,
       },
       user: {
         port: 6379,          // Redis port
         host: '127.0.0.1',   // Redis host
         password: '',
         db: 1,
       } */
    },
  };

  config.multipart = {
    mode: 'file',
    fileSize: '50mb',
  },

    config.session = {
      key: 'SESSION_ID',
      maxAge: 24 * 3600 * 1000,  // 过期时间
      httpOnly: true,
      encrypt: true,
      renew: true // 延长会话有效期 
    }

  return config;
};
